package game;

import game.manager.PersonManager;
import game.manager.RelationshipManager;

import java.util.Collection;
import java.util.List;
import java.util.Vector;

public class Conversation {
	private List<Message> messages;
	private Collection<Person> participants;

	public Conversation(Person aPerson, Person bPerson) {
		this.messages = new Vector<Message>();
		this.participants = new Vector<Person>(2);
		this.participants.add(aPerson);
		this.participants.add(bPerson);
	}

	public List<Message> getMessages() {
		return new Vector<Message>(messages);
	}

	public boolean isBetween(Person cPerson, Person dPerson) {
		final Collection<Person> bParticipants = new Vector<Person>(2);
		bParticipants.add(cPerson);
		bParticipants.add(dPerson);
		return participants.containsAll(bParticipants);
	}

	public Person getOtherParticipant(Person participant) {
		for (Person aParticipant : this.participants) {
			if (!aParticipant.equals(participant))
				return aParticipant;
		}
		throw new IllegalStateException("Could not find other participant");
	}

	public String getTitle() {
		String title = "";
		Person user = PersonManager.getManager().getUser();
		title = getOtherParticipant(user).getFullName();
		return title;
	}

	public void addMessage(Message message) {
		final Person sender = message.getSender();
		final Person receiver = message.getReceiver();
		if (isBetween(sender, receiver)) {
			if (this.messages.isEmpty())
				RelationshipManager.getManager().addRelationship(receiver,
						sender);
			this.messages.add(message);
		} else
			throw new IllegalArgumentException(
					"Message not for this conversation!");
	}

	public List<Person> getParticipants() {
		return new Vector<Person>(this.participants);
	}
}
